import { defineStore } from "pinia";
import router from "@/router";
import { ClassTag } from "#/index";
import type { RouteLocationNormalizedLoaded } from "vue-router";

export const useTagsStore = defineStore({
    id: 'TagsStore',
    persist: {
        paths:['tagList']
    }, //持久化
    state: () => ({
        tagList: [new ClassTag("/index/dashboard", "home", "Dashboard")]//打开的标签列表
    }),
    actions: {
        /**
         * 添加标签
         * @param router 路由对象
         * @param index 要添加的位置,默认添加到末尾
         * @returns 
         */
        pushTag(router: RouteLocationNormalizedLoaded, index: number = -1) {
            const { fullPath, meta, name, } = router
            if ("redirect" === name) {  //过滤重定向页面
                return;
            }
            let openTags = this.tagList;
            let findTags = openTags.find((item: ClassTag) => item.fullPath == fullPath)
            if (!findTags) {
                var i = index > -1 ? index : openTags.length
                openTags.splice(i, 0, new ClassTag(fullPath, meta.title as string, name as string))
            }
        },
        /**
         * 删除标签
         * @param index 标签索引位置
         */
        removeTag(index: number) {
            let openTags = this.tagList;
            let tag = openTags[index];
            let route = router.currentRoute.value; //当前路由
            //删除当前激活的标签,优先显示后面的标签,如果没有就显示前面的标签
            if (tag.fullPath == route.fullPath) {
                let { fullPath } = openTags[index + 1] || openTags[index - 1]
                router.push(fullPath)
            }
            openTags.splice(index, 1)
        },
        /**
         * 根据路劲删除标签
         * @param fullPath 要删除的路由路劲
         */
        removeTagBaPath(fullPath: string) {
            let openTags = this.tagList;
            let index = openTags.findIndex(item => item.fullPath == fullPath);
            if (-1 != index) {
                this.removeTag(index) //异步调用actions的removeTag方法
            }
        },
        /**
         * 点击标签
         * @param index 标签索引位置
         */
        clickTag(index: number) {
            const { fullPath } = this.tagList[index];
            let route = router.currentRoute.value;//当前路由
            if (fullPath != route.fullPath) {
                router.push(fullPath)
            }
        },
        /**
         * 关闭所有
         */
        closeAll() {
            this.tagList.splice(1);
            router.push({ name: 'Dashboard' })
        },
        /**
         * 删除索引所在的右侧标签
         * @param index 标签索引位置
         */
        closeRight(index: number) {
            let openTags = this.tagList;
            let actvieIndex = openTags.findIndex(item => item.fullPath == router.currentRoute.value.fullPath); //默认选中的下标
            openTags.splice(index + 1)
            if (actvieIndex > index) { //当前页面被删除,显示右击菜单页
                router.push(openTags[index].fullPath)
            }
        },
        /**
         * 刷新
         * @param index 标签索引位置
         */
        refresh(index: number) {
            const { fullPath } = this.tagList[index];
            router.push('/index/redirect/' + fullPath);
        }
    }
})